package com.ruoyi.hr.controller.hr.welfare.welfareopen;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.hr.domain.Authority;
import com.ruoyi.hr.domain.welfare.welfareopen.WelfareOpen;
import com.ruoyi.hr.service.AuthorityService;
import com.ruoyi.hr.service.SkyMsgAlarmService;
import com.ruoyi.hr.service.welfare.welfareRegion.WelfareRegionConfigService;
import com.ruoyi.hr.service.welfare.welfareopen.WelfareOpenService;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author zeo
 * @explain 福利开启
 * @date 2019/12/30 14:01
 */
@Controller
@RequestMapping("/welfare/welfareOpen")
public class WelfareOpenController extends BaseController {

    @Autowired
    WelfareOpenService welfareOpenService;

    @Autowired
    WelfareRegionConfigService welfareRegionConfigService;

    @Autowired
    SkyMsgAlarmService skyMsgAlarmService;

    @Autowired
    ISysUserService iSysUserService;

    @Autowired
    AuthorityService authorityService;

    private String prefix = "hr/welfare/welfareopen/welfareopen";

    @GetMapping()
    public String welfareopen() {
        return prefix + "/welfareopen";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(WelfareOpen welfareOpen) {
        SysUser user = ShiroUtils.getSysUser();
        List<Authority> authorityList = authorityService.getAuthorityUser(user.getUserId().toString());
        int isAdmin = 0;
        if (authorityList.size() == 1) {
            if (authorityList.get(0).getIsAdmin() != null) {
                isAdmin = 1;
            }
        }
        startPage();
        List<WelfareOpen> list = welfareOpenService.findData(welfareOpen,authorityList,isAdmin);
        return getDataTable(list);
    }

    @GetMapping("edit/{id}")
    public String welfareOpen(@PathVariable("id") int id, ModelMap mmap) {
        mmap.put("welfareOpen", welfareOpenService.findWelfareOpen(id));

        mmap.put("welfareRegionConfigs", welfareRegionConfigService.findAlla());
        return prefix + "/edit";
    }

    /**
     * 修改操作
     *
     * @param welfareOpen
     * @return
     */
    @PostMapping("edit")
    @ResponseBody
    public AjaxResult edit(WelfareOpen welfareOpen) {
        return toAjax(welfareOpenService.updateWelfareOpen(welfareOpen));
    }

    /**
     * (确认检查,取消确认,福利处理)
     */
    @PostMapping("/procedureRegister")
    @ResponseBody
    public String procedureRegister(String ids, String procedure) {
        List<WelfareOpen> welfareOpens = welfareOpenService.findDataByIds(ids);
        String sql;//存储过程语句
        String msg;//异常信息
        //叠加异常信息
        String msgAdd = "";
        List<List<Map<String, Object>>> list;
        SysUser user = ShiroUtils.getSysUser();
        Map<String, Object> maps = iSysUserService.selectSkySecUser(user.getLoginName());
        for (WelfareOpen welfareOpen : welfareOpens) {
            sql = "declare @RetVal varchar(50) exec " + procedure + " " + welfareOpen.getId() + "," + maps.get("id").toString() + ",@RetVal output select @RetVal";
            list = welfareOpenService.executeProcedure(sql);

            if (list.size() > 1) {

            } else {
                String mgsId = list.toString();
                msg = skyMsgAlarmService.getMsgById(mgsId.substring(3, mgsId.length() - 2));
                if (msg != null) {
                    msgAdd += welfareOpen.getName() + ":" + msg + "\n";
                }
            }
        }
        return msgAdd;
    }
}

